package com.bstek.demo.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bstek.demo.dao.UserDao;
import com.bstek.demo.entity.MyPage;
import com.bstek.demo.entity.User;
import com.bstek.demo.entity.UserMessage;
import com.bstek.demo.service.UserService;
import com.bstek.demo.utils.CommonUtils;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;
import com.bstek.dorado.web.DoradoContext;
/**
 * 用户信息实现类
 * @author lemon
 *
 */
@Service
public class UserServiceImpl implements UserService{

	@Autowired UserDao userDao;
	/**
	 * 获取用户信息
	 */
	@Override
	public void getAll(Page<User> page,Criteria criteria,User user) {
		 // 设置EntityConut属性值 总条数
		page.setEntityCount(userDao.getCount());
		Map<String,List<String[]>> filterParams = CommonUtils.getFilterParamUtils(criteria,User.class);
		MyPage myPage = CommonUtils.getPageParams(page);
		List<User> list = userDao.getAll(myPage,filterParams,user);
		// 设置Entities值 数据集合
		page.setEntities(list);
	}
	/**
	 * 持久化User操作
	 */
	@Override
	@Transactional
	public void saveAll(Collection<User> users) {
		for(User user : users){
			System.out.println(EntityUtils.getState(user));
			userDao.saveAll(user);
		}
	}
	/** ֤
	 * 登陆检测方法
	 */
	@Override
	public boolean isValue(User user) {
		boolean flg = true;
		User resultUser = userDao.getUserByName(user);
		if(resultUser == null || !resultUser.getPassword().equals(user.getPassword())){
			flg = false;
		}else{
			// 设置session方法
			DoradoContext ctx = DoradoContext.getCurrent();
            HttpServletRequest request = ctx.getRequest();
            request.getSession().setAttribute("user", resultUser);
		}
		return flg;
	}
	/**
	 * 根据用户名 获取当前用户信息
	 */
	@Override
	public User getMyInfo(String username) {
		User user = new User();
		user.setUsername(username);
		User result = userDao.getUserByName(user);
		result.setUserMessageSet(userDao.getUserMessage(user));
		return result;
	}
	/**
	 * 持久化用户明细信息
	 */
	@Override
	@Transactional
	public void saveMessages(Collection<User> user) {
		for(User u : user){
			for(UserMessage userMessage : u.getUserMessageSet()){
				userDao.saveMessage(u, userMessage);
			}
		}
		
	}
	/**
	 * 分页获取所有的用户明细信息
	 */
	@Override
	public void getAllMessage(Page<User> page) {
		 // 设置总条数
		page.setEntityCount(userDao.getCount());
		MyPage myPage = CommonUtils.getPageParams(page);
		List<User> list = userDao.getAllMessage(myPage);
		for(User u : list){
			// 设置用户的UserMessgeSet属性 当前做法会有性能问题 优化采用懒加载
			u.setUserMessageSet(userDao.getUserMessage(u));
		}
		// 设置结果集
		page.setEntities(list);
	}
	@Override
	public void getAllMessage2(Page<User> page){
		page.setEntityCount(userDao.getCount());
		MyPage myPage = CommonUtils.getPageParams(page);
		List<User> list = userDao.getAllMessage(myPage);
		page.setEntities(list);		
	}
	/**
	 * 根据用户名获取用户明细
	 */
	@Override
	public Collection<UserMessage> getUserMessageByUsername(String username) {
		return userDao.getUserMessageByUsername(username);
	}
	
}
